package com.tykboot.controller;

import com.alibaba.fastjson.JSON;
import com.tykboot.core.base.BaseController;
import com.tykboot.core.persistence.Criteria;
import com.tykboot.core.persistence.Restrictions;
import com.tykboot.entity.TbDirectDebit;
import com.tykboot.entity.TbProduct;
import com.tykboot.message.MsgCode;
import com.tykboot.message.ResponseMsg;
import com.tykboot.service.ITbDirectDebitService;
import com.tykboot.vo.DirectDebitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  Controller
 *
 * @author LiuYu
 */ 
@Api(tags = "direct-debit", description = "/api/direct-debits")
@RestController
@RequestMapping(BaseController.API + "/direct-debits")
public class TbDirectDebitController extends BaseController {

	@Autowired
	private ITbDirectDebitService tbDirectDebitService;

	@ApiOperation(value = "Add an direct-debit", notes = "Add an direct-debit")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@PostMapping("")
	public ResponseMsg<TbDirectDebit> add(@RequestHeader("financialId") String financialId,
									  @RequestBody DirectDebitVo requestBody) {
		ResponseMsg<TbDirectDebit> responseMsg = new ResponseMsg<>();
		TbDirectDebit entity = new TbDirectDebit();
		BeanUtils.copyProperties(requestBody, entity);
		entity.setFinancialId(financialId);
		entity.setPreviousPaymentAmountDb(JSON.toJSONString(requestBody.getPreviousPaymentAmount()));
		entity = tbDirectDebitService.save(entity);
		responseMsg.setData(entity);
		return responseMsg;
	}

	@ApiOperation(value = "Update an direct-debit", notes = "Update an direct-debit")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "directDebitId", value = "directDebitId", dataType = "string", paramType = "path", required = true)
	})
	@PutMapping("/{directDebitId}")
	public ResponseMsg<TbDirectDebit> update(@RequestHeader("financialId") String financialId,
										 @PathVariable("directDebitId") String directDebitId,
										 @RequestBody DirectDebitVo requestBody) {
		ResponseMsg<TbDirectDebit> responseMsg = new ResponseMsg<>();
		TbDirectDebit entity = tbDirectDebitService.findById(directDebitId);
		if (entity == null) {
			return new ResponseMsg<>(MsgCode.Param_Error);
		}
		BeanUtils.copyProperties(requestBody, entity, "directDebitId");
		entity.setFinancialId(financialId);
		entity.setPreviousPaymentAmountDb(JSON.toJSONString(entity.getPreviousPaymentAmount()));
		entity = tbDirectDebitService.save(entity);
		responseMsg.setData(entity);
		return responseMsg;
	}

	@ApiOperation(value = "Get Direct Debits", notes = "Get Direct Debits")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@GetMapping("")
	public ResponseMsg<List<TbDirectDebit>> queryDirectDebits(@RequestHeader("financialId") String financialId) {
		ResponseMsg<List<TbDirectDebit>> responseMsg = new ResponseMsg<>();
		Criteria<TbDirectDebit> req = new Criteria<>();
		req.add(Restrictions.eq("financialId", financialId));
		List<TbDirectDebit> list = tbDirectDebitService.findAll(req);
		responseMsg.setData(list);
		return responseMsg;
	}

	@ApiOperation(value = "Get account Direct Debits", notes = "Get Direct Debits related to an account")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "accountId", value = "accountId", dataType = "string", paramType = "path", required = true)
	})
	@GetMapping("/{accountId}")
	public ResponseMsg<List<TbDirectDebit>> queryDirectDebitsById(@RequestHeader("financialId") String financialId,
																  @PathVariable("accountId") String accountId) {
		ResponseMsg<List<TbDirectDebit>> responseMsg = new ResponseMsg<>();
		Criteria<TbDirectDebit> req = new Criteria<>();
		req.add(Restrictions.eq("accountId", accountId));
		req.add(Restrictions.eq("financialId", financialId));
		List<TbDirectDebit> list = tbDirectDebitService.findAll(req);
		responseMsg.setData(list);
		return responseMsg;
	}

}
